/*******************************************************************

    Author: Xiao
    Date: 2022/3/28 17:19

*******************************************************************/

package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"mcr/pkg/config"
	"mcr/pkg/dao"
	"mcr/pkg/logs"
	"mcr/pkg/middleware"
	"mcr/pkg/utils"
	socket "mcr/pkg/websocket"
	"reflect"
	"strconv"
	"strings"
	"time"
)

type LoginReq struct {
	Plat         string `json:"plat" binding:"required"`
	Version      int    `json:"version" binding:"required"`
	UserAccount  string `json:"user_account" binding:"required"`
	UserPassword string `json:"user_password" binding:"required"`
	UserDevice   string `json:"user_device"`
}

// 用户登录接口
func LoginUser(ctx *gin.Context) {
	lm := LoginReq{}
	err := ctx.ShouldBindJSON(&lm)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	var flag, ban bool
	var uid int
	var mail string
	var have bool
	// 获取是否有邮件验证码
	verify, err := dao.GetMailVerify(lm.UserAccount + "(" + lm.Plat + ")")
	if err == nil && verify == lm.UserPassword {
		flag, ban, uid = dao.GetUserIdByAccount(lm.UserAccount, ctx.ClientIP(), lm.UserDevice)
		mail = lm.UserAccount
		dao.DelMailVerify(lm.UserAccount + "(" + lm.Plat + ")")
	} else {
		flag, ban, uid, mail, have = dao.LoginByPassword(lm.UserAccount, lm.UserPassword)
		if !flag {
			if config.ConfigValue.SimpleRegister && lm.UserPassword == "123456" {
				if !have {
					flag, ban, uid = dao.GetUserIdByAccount(lm.UserAccount, ctx.ClientIP(), lm.UserDevice)
					mail = lm.UserAccount
				} else {
					ctx.JSON(200, gin.H{
						"code": 500,
						"msg":  "帐号已存在，密码错误",
					})
					return
				}
			}
		}
	}
	if flag {
		if ban {
			ctx.JSON(200, gin.H{
				"msg": fmt.Sprintf("你的账户被禁用,登录失败"),
			})
			return
		}
		token, err := middleware.CreateToken(mail, lm.Plat, uid, time.Duration(config.ConfigValue.JwtExpire)*time.Hour)
		if err != nil {
			logs.Log.Errorf("获取用户Token错误:%s", err)
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  fmt.Sprintf("登录系统异常"),
			})
			return
		}
		// 设置token 至redis
		err = dao.SetToken(mail+"("+lm.Plat+")", token)
		if err != nil {
			logs.Log.Errorf("设置用户Token错误:%s", err)
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  fmt.Sprintf("登录系统异常"),
			})
			return
		}
		err = dao.CreateAccess(uid, lm.Plat, token, ctx.ClientIP())
		if err != nil {
			logs.Log.Errorf("记录登录信息错误:%s", err)
		}
		//err = dao.UpdateUserDevice(uid,lm.UserDevice)
		//if err != nil{
		//	logs.Log.Errorf("更新用户设备:%s",err)
		//}
		data := make(map[string]interface{})
		data["access_token"] = token
		ctx.SetCookie("access_token", token, 3600, "/", "localhost", false, true)
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "登录成功",
			"data": data,
		})
		return
	} else {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "帐号或密码错误",
		})
		return
	}
}

// 用户信息
type UserInfo struct {
	PassCount    int         `json:"pass_count"`
	PushCount    int         `json:"push_count"`
	UserAdmin    bool        `json:"user_admin"`
	UserId       int         `json:"user_id"`
	UserIcon     int         `json:"user_icon"`
	UserSex      int         `json:"user_sex"`
	UserName     string      `json:"user_name"`
	UserHead     string      `json:"user_head"`
	UserRemark   string      `json:"user_remark"`
	UserExtra    string      `json:"user_extra"`
	UserDevice   string      `json:"user_device"`
	UserTouchtip string      `json:"user_touchtip"`
	UserVip      string      `json:"user_vip"`
	UserGroup    int         `json:"user_group"`
	MyRoom       interface{} `json:"myRoom"`
	UserShutdown bool        `json:"user_shutdown"`
	UserSongdown bool        `json:"user_songdown"`
	UserGuest    bool        `json:"user_guest"`
}

// 获取指定用户资料
func GetUserInfo(ctx *gin.Context) {
	json := make(map[string]interface{}) //注意该结构接受的内容
	err := ctx.BindJSON(&json)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	if uid, ok := json["user_id"]; ok {
		if uid == 0.0 {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "该用户为游客，没有用户信息.",
			})
			return
		}
		if reflect.TypeOf(uid).Name() == "float64" {
			u, err := dao.GetUserByUserId(int(uid.(float64)))
			if err != nil {
				ctx.JSON(200, gin.H{
					"code": 500,
					"msg":  "没找到指定用户",
				})
				return
			}
			ui := UserInfo{
				UserId:       u.UserId,
				UserTouchtip: u.UserTouchtip,
				UserSex:      u.UserSex,
				UserRemark:   u.UserRemark,
				UserHead:     u.UserHead,
				UserName:     u.UserName,
				UserDevice:   u.UserDevice,
				UserExtra:    u.UserExtra,
				UserIcon:     u.UserIcon,
				UserVip:      u.UserVip,
				UserAdmin:    utils.GetIsAdmin(u.UserGroup),
			}
			r, err := dao.GetRoomByUserId(u.UserId)
			if err == nil {
				ui.MyRoom = r
			}
			if pcStr, err := dao.GetCacheString("pass_song_card_user_" + strconv.Itoa(u.UserId)); err == nil {
				if pc, err := strconv.Atoi(pcStr); err == nil {
					ui.PassCount = pc
				}
			}
			if pcStr, err := dao.GetCacheString("push_song_card_user_" + strconv.Itoa(u.UserId)); err == nil {
				if pc, err := strconv.Atoi(pcStr); err == nil {
					ui.PushCount = pc
				}
			}
			ctx.JSON(200, gin.H{
				"code": 200,
				"msg":  "获取成功",
				"data": ui,
			})
			return
		}
	}
	ctx.JSON(200, gin.H{
		"code": 400,
		"msg":  "参数不合法",
	})
	return
}

type onlineReq struct {
	BasicReq
	RoomId int `json:"room_id" binding:"required"`
}

// 获取在线用户
func GetOnlines(ctx *gin.Context) {
	or := onlineReq{}
	err := ctx.ShouldBindJSON(&or)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	cs := socket.GetClientsByRoom(or.RoomId)
	var users []UserInfo
	for _, c := range cs {
		var u UserInfo
		uid, err := strconv.Atoi(c.UserId)
		if err != nil {
			u.UserName = "来自" + utils.GetAddrPath(c.Addr) + "的游客"
			u.UserHead = config.ConfigValue.FrontendUrl + "new/images/nohead.jpg"
			users = append(users, u)
		} else {
			u, err = GetUserData(uid)
			if err != nil {
				logs.Log.Errorf("未找到指定用户:%s", err)
				continue
			}
			if _, err = dao.GetCacheString(fmt.Sprintf("shutdown_room_%d_user_%d", or.RoomId, u.UserId)); err == nil {
				u.UserShutdown = true
			}
			if _, err = dao.GetCacheString(fmt.Sprintf("songdown_room_%d_user_%d", or.RoomId, u.UserId)); err == nil {
				u.UserSongdown = true
			}
			if _, err = dao.GetCacheString(fmt.Sprintf("guest_room_%d_user_%d", or.RoomId, u.UserId)); err == nil {
				u.UserGuest = true
			}
			users = append(users, u)
		}
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"data": users,
	})
	return
}

// 获取自身资料
func GetMyInfo(ctx *gin.Context) {
	var bq BasicReq
	err := ctx.BindJSON(&bq)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}

	if bq.AccessToken == config.ConfigValue.GuestToken {
		ui := UserInfo{
			UserId:       -1,
			UserTouchtip: "Ghost",
			UserHead:     "new/images/nohead.jpg",
			UserAdmin:    false,
			MyRoom:       false,
		}
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "获取成功",
			"data": ui,
		})
		return
	}

	uid, err := middleware.GetUserId(bq.AccessToken)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err,
		})
		return
	}
	u, err := dao.GetUserByUserId(uid)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "没找到指定用户",
		})
		return
	}
	ui := UserInfo{
		UserId:       u.UserId,
		UserTouchtip: u.UserTouchtip,
		UserSex:      u.UserSex,
		UserRemark:   u.UserRemark,
		UserHead:     u.UserHead,
		UserName:     u.UserName,
		UserDevice:   u.UserDevice,
		UserExtra:    u.UserExtra,
		UserIcon:     u.UserIcon,
		UserVip:      u.UserVip,
		UserAdmin:    utils.GetIsAdmin(u.UserGroup),
	}
	r, err := dao.GetRoomByUserId(u.UserId)
	if err == nil {
		ui.MyRoom = r
	} else {
		ui.MyRoom = false
	}
	if pcStr, err := dao.GetCacheString("pass_song_card_user_" + strconv.Itoa(u.UserId)); err == nil {
		if pc, err := strconv.Atoi(pcStr); err == nil {
			ui.PassCount = pc
		}
	}
	if pcStr, err := dao.GetCacheString("push_song_card_user_" + strconv.Itoa(u.UserId)); err == nil {
		if pc, err := strconv.Atoi(pcStr); err == nil {
			ui.PushCount = pc
		}
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "获取成功",
		"data": ui,
	})
	return
}

type UpdateUserReq struct {
	BasicReq
	UserName     string `json:"user_name" binding:"required"`
	UserHead     string `json:"user_head" binding:"required"`
	UserRemark   string `json:"user_remark" `
	UserTouchtip string `json:"user_touchtip"`
	UserSex      int    `json:"user_sex" `
	UserPassword string `json:"user_password"`
}

// 修改用户信息
func UpdateUserInfo(ctx *gin.Context) {
	uur := UpdateUserReq{}
	err := ctx.ShouldBindJSON(&uur)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	if uur.AccessToken == config.ConfigValue.GuestToken {
		ctx.JSON(200, gin.H{
			"code": 401,
			"msg":  "请登录后体验完整功能!",
		})
		return
	}
	if uur.UserName == "" {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "你确定飘到连名字都可以不要了吗？",
		})
		return
	}
	if uur.UserHead != "" {
		if strings.Contains(uur.UserHead, ".gif") {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "头像不支持Gif,不要尝试钻空子",
			})
			return
		}
	}
	successMsg := "资料更新成功"
	column := make(map[string]interface{})
	if uur.UserPassword != "" {
		if len(uur.UserPassword) < 6 || len(uur.UserPassword) > 16 {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "新密码应为6-16位！",
			})
			return
		}
		salt := utils.GetVerifyEnglishCode(4)
		password := utils.EncodeBySalt(uur.UserPassword, salt)
		successMsg = "更新成功,请下次使用新密码登录!"
		column["user_password"] = password
		column["user_salt"] = salt
	}
	uid, err := middleware.GetUserId(uur.AccessToken)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "无效的Token",
		})
		return
	}
	column["user_name"] = uur.UserName
	column["user_head"] = uur.UserHead
	column["user_remark"] = uur.UserRemark
	column["user_sex"] = uur.UserSex
	column["user_touchtip"] = uur.UserTouchtip
	err = dao.UpdateUserInfo(uid, column)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "资料更新失败",
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  successMsg,
	})
}

// 获取用户返回信息
func GetUserData(uid int) (UserInfo, error) {
	ui := UserInfo{}
	u, err := dao.GetUserByUserId(uid)
	if err != nil {
		return ui, err
	}
	ui = UserInfo{
		UserId:       u.UserId,
		UserTouchtip: u.UserTouchtip,
		UserSex:      u.UserSex,
		UserRemark:   u.UserRemark,
		UserHead:     u.UserHead,
		UserName:     u.UserName,
		UserDevice:   u.UserDevice,
		UserExtra:    u.UserExtra,
		UserIcon:     u.UserIcon,
		UserVip:      u.UserVip,
		UserGroup:    u.UserGroup,
		UserAdmin:    utils.GetIsAdmin(u.UserGroup),
	}
	return ui, nil
}

type RoomUserOperateReq struct {
	BasicReq
	UserId int `json:"user_id"`
	RoomId int `json:"room_id"`
}

// 嘉宾的控件
func PostGuestctrl(ctx *gin.Context) {
	ruor := RoomUserOperateReq{}
	err := ctx.ShouldBindJSON(&ruor)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	if ruor.AccessToken == config.ConfigValue.GuestToken {
		ctx.JSON(200, gin.H{
			"code": 401,
			"msg":  "请登录后体验完整功能!",
		})
		return
	}
	uid, err := middleware.GetUserId(ruor.AccessToken)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err,
		})
		return
	}
	operateUser, err := GetUserData(uid)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "操作用户信息查询失败",
		})
		return
	}
	if uid == ruor.UserId {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "自己给自己设置嘉宾是不是太没意思了?",
		})
		return
	}
	r, err := dao.GetRoomById(ruor.RoomId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "房间信息查询失败",
		})
		return
	}
	u, err := GetUserData(ruor.UserId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户信息查询失败",
		})
		return
	}
	if u.UserId == r.RoomUser {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "他是房主,你没必要给他设置嘉宾",
		})
		return
	}
	if utils.GetIsAdmin(operateUser.UserGroup) {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "你无权操作管理员",
		})
		return
	}
	if r.RoomUser != operateUser.UserId && !utils.GetIsAdmin(operateUser.UserGroup) {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "你无权操作",
		})
		return
	}
	sendMsg := make(map[string]interface{})
	cacheName := fmt.Sprintf("guest_room_%d_user_%d", ruor.RoomId, ruor.UserId)
	if _, err := dao.GetCacheString(cacheName); err != nil {
		err := dao.SetCacheString(cacheName, time.Now().Nanosecond(), 0)
		if err != nil {
			logs.Log.Errorf("设置嘉宾失败:%s", err)
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "设置嘉宾失败",
			})
			return
		}
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "设置嘉宾成功",
		})
		sendMsg["type"] = "guest_add"
	} else {
		dao.DelCacheString(cacheName)
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "取消嘉宾身份成功",
		})
		sendMsg["type"] = "guest_remove"
	}
	sendMsg["time"] = time.Now().Format("15:04:05")
	sendMsg["user"] = operateUser
	sendMsg["guest"] = u
	socket.SendMsgToRoom(ruor.RoomId, sendMsg)
	return
}

// 禁言
func PostShutdown(ctx *gin.Context) {
	ruor := RoomUserOperateReq{}
	err := ctx.ShouldBindJSON(&ruor)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	if ruor.AccessToken == config.ConfigValue.GuestToken {
		ctx.JSON(200, gin.H{
			"code": 401,
			"msg":  "请登录后体验完整功能!",
		})
		return
	}
	uid, err := middleware.GetUserId(ruor.AccessToken)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err,
		})
		return
	}
	operateUser, err := GetUserData(uid)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "操作用户信息查询失败",
		})
		return
	}
	if uid == ruor.UserId {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "自己禁止自己,开玩笑把?",
		})
		return
	}
	r, err := dao.GetRoomById(ruor.RoomId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "房间信息查询失败",
		})
		return
	}
	u, err := GetUserData(ruor.UserId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户信息查询失败",
		})
		return
	}
	// 是否为超管
	if operateUser.UserId > 1 {
		if r.RoomUser != operateUser.UserId && !utils.GetIsAdmin(operateUser.UserGroup) {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "你无权操作",
			})
			return
		}
		if utils.GetIsAdmin(u.UserGroup) || ruor.UserId == r.RoomUser || u.UserId <= 1 {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "你无权操作管理员",
			})
			return
		}
	}
	cacheName := fmt.Sprintf("shutdown_room_%d_user_%d", ruor.RoomId, ruor.UserId)
	err = dao.SetCacheString(cacheName, time.Now().Nanosecond(), 0)
	if err != nil {
		logs.Log.Errorf("禁止发言失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "禁止发言失败",
		})
		return
	}
	sendMsg := make(map[string]interface{})
	sendMsg["type"] = "shutdown"
	sendMsg["time"] = time.Now().Format("15:04:05")
	sendMsg["user"] = operateUser
	sendMsg["ban"] = u
	socket.SendMsgToRoom(ruor.RoomId, sendMsg)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "禁言成功",
	})
	return
}

// 禁止点歌
func PostSongDown(ctx *gin.Context) {
	ruor := RoomUserOperateReq{}
	err := ctx.ShouldBindJSON(&ruor)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	if ruor.AccessToken == config.ConfigValue.GuestToken {
		ctx.JSON(200, gin.H{
			"code": 401,
			"msg":  "请登录后体验完整功能!",
		})
		return
	}
	uid, err := middleware.GetUserId(ruor.AccessToken)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err,
		})
		return
	}
	operateUser, err := GetUserData(uid)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "操作用户信息查询失败",
		})
		return
	}
	if uid == ruor.UserId {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "自己禁止自己,开玩笑把?",
		})
		return
	}
	r, err := dao.GetRoomById(ruor.RoomId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "房间信息查询失败",
		})
		return
	}
	u, err := GetUserData(ruor.UserId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户信息查询失败",
		})
		return
	}
	// 是否为超管
	if operateUser.UserId > 1 {
		if r.RoomUser != operateUser.UserId && !utils.GetIsAdmin(operateUser.UserGroup) {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "你无权操作",
			})
			return
		}
		if utils.GetIsAdmin(u.UserGroup) || ruor.UserId == r.RoomUser || u.UserId <= 1 {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "你无权操作管理员",
			})
			return
		}
	}
	cacheName := fmt.Sprintf("songdown_room_%d_user_%d", ruor.RoomId, ruor.UserId)
	err = dao.SetCacheString(cacheName, time.Now().Nanosecond(), 0)
	if err != nil {
		logs.Log.Errorf("禁止点歌失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "禁止点歌失败",
		})
		return
	}
	sendMsg := make(map[string]interface{})
	sendMsg["type"] = "songdown"
	sendMsg["time"] = time.Now().Format("15:04:05")
	sendMsg["user"] = operateUser
	sendMsg["ban"] = u
	socket.SendMsgToRoom(ruor.RoomId, sendMsg)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "禁止点歌成功",
	})
	return
}

// 解禁
func PostRemoveBan(ctx *gin.Context) {
	ruor := RoomUserOperateReq{}
	err := ctx.ShouldBindJSON(&ruor)
	if err != nil {
		logs.Log.Errorf("获取用户请求参数失败:%s", err)
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  "参数不合法",
		})
		return
	}
	if ruor.AccessToken == config.ConfigValue.GuestToken {
		ctx.JSON(200, gin.H{
			"code": 401,
			"msg":  "请登录后体验完整功能!",
		})
		return
	}
	uid, err := middleware.GetUserId(ruor.AccessToken)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 400,
			"msg":  err,
		})
		return
	}
	operateUser, err := GetUserData(uid)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "操作用户信息查询失败",
		})
		return
	}
	if uid == ruor.UserId {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "自己禁止自己,开玩笑把?",
		})
		return
	}
	r, err := dao.GetRoomById(ruor.RoomId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "房间信息查询失败",
		})
		return
	}
	u, err := GetUserData(ruor.UserId)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户信息查询失败",
		})
		return
	}
	// 是否为超管
	if operateUser.UserId > 1 {
		if r.RoomUser != operateUser.UserId && !utils.GetIsAdmin(operateUser.UserGroup) {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "你无权操作",
			})
			return
		}
		if utils.GetIsAdmin(u.UserGroup) || ruor.UserId == r.RoomUser || u.UserId <= 1 {
			ctx.JSON(200, gin.H{
				"code": 500,
				"msg":  "你无权操作管理员",
			})
			return
		}
	}
	dao.DelCacheString(fmt.Sprintf("shutdown_room_%d_user_%d", ruor.RoomId, ruor.UserId))
	dao.DelCacheString(fmt.Sprintf("songdown_room_%d_user_%d", ruor.RoomId, ruor.UserId))
	sendMsg := make(map[string]interface{})
	sendMsg["type"] = "removeban"
	sendMsg["time"] = time.Now().Format("15:04:05")
	sendMsg["user"] = operateUser
	sendMsg["ban"] = u
	socket.SendMsgToRoom(ruor.RoomId, sendMsg)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "用户解禁成功",
	})
	return
}
